package com.yinp.proapp.view;

import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yinp.proapp.utils.AppUtils;

import java.util.List;

/**
 * @author :yinpeng
 * date      :2021/8/19
 * package   :com.yinp.proapp.view
 * describe  :一个通用的瀑布流布局器
 */
public class GridItemDecoration extends RecyclerView.ItemDecoration {
    private Context context;
    private List<Point> list;
    private int count = 0;
    private int top = -1;

    public GridItemDecoration(Context context, List<Point> list) {
        this.context = context;
        this.list = list;
        count = 0;
    }

    public GridItemDecoration(Context context, List<Point> list, int top) {
        this.context = context;
        this.list = list;
        this.top = top;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        outRect.left = AppUtils.dpToPx(list.get(count).x);
        outRect.right = AppUtils.dpToPx(list.get(count).y);
        if (top != -1) {
            outRect.top = AppUtils.dpToPx(top);
        }
        if (++count == list.size()) {
            count = 0;
        }
    }
}